
class Main{
    constructor(){
        let m={
            allSunVal:200,      // 阳光总数量
            loading:null,       // loading 动画对象
            sunnum:null,        // 阳光实例对象
            cars:[],            // 实例化除草车对象数组
            cars_info:{         // 初始化参数
                x:170,          // x 轴坐标
                y:102,          // y 轴坐标
                position:[
                    {row:1},
                    {row:2},
                    {row:3},
                    {row:4},
                    {row:5},
                ],
            },
            cards:[],
            cards_info:{
                x:0,
                y:0,
                position:[
                    {name: 'sunflower', row: 1, sun_val: 50, timer_spacing: 5 * 1000},
                    {name: 'wallnut', row: 2, sun_val: 50, timer_spacing: 12 * 1000},
                    {name: 'peashooter', row: 3, sun_val: 100, timer_spacing: 7 * 1000},
                    {name: 'repeater', row: 4, sun_val: 150, timer_spacing: 10 * 1000},
                    {name: 'gatlingpea', row: 5, sun_val: 200, timer_spacing: 15 * 1000},
                    {name: 'chomper', row: 6, sun_val: 200, timer_spacing: 15 * 1000},
                    {name: 'cherrybomb', row: 7, sun_val: 250, timer_spacing: 25 * 1000},
                ]
            },
            plants:[],
            zombies:[],
            plants_info:{
                type:'plant',
                x:250,
                y:92,
                position:[]
            },
            zombies_info:{
                type:'zombie',
                x:170,
                y:15,
                position:[]
            },
            zombies_idx: 0,                           
            zombies_row: 0,                            
            zombies_iMax: 50,                          
            sunTimer: null,                            
            sunTimer_difference: 20,                   
            zombieTimer: null,                         
            zombieTimer_difference: 12,                
            game: null,                            
            fps: 60,
        }
        Object.assign(this,m)
    }
    setZombiesInfo () {
        let self = this,
            iMax = self.zombies_iMax
        for(let i = 0; i < iMax; i++) {
            let row = Math.ceil(Math.random() * 4 + 1)
            self.zombies_info.position.push({
                section: 'zombie',
                row: row,
                col: 11 + Number(Math.random().toFixed(1))
            })
        }
    }

    clearTiemr(){
        let self=this
        clearInterval(self.sunTimer)
        clearInterval(self.zombieTimer)
        for(let plant of self.plants){
            if(plant.section=='sunflower'){
                plant.clearSunTimer()
            }
        }
    }
    // 设置全局阳光、僵尸生成定时器
    setTimer(){
        let self=this,zombies=self.zombies
        self.sunTimer = setInterval(function () {
            let left = parseInt(window.getComputedStyle(document.getElementsByClassName('systemSun')[0],null).left), // 获取当前元素left值
                top = '-100px',
                keyframes1 = [
                    { transform: 'translate(0,0)', opacity: 0 },
                    { offset: .5,transform: 'translate(0,300px)', opacity: 1 },
                    { offset: .75,transform: 'translate(0,300px)', opacity: 1 },
                    { offset: 1,transform: 'translate(-'+ (left - 110) +'px,50px)',opacity: 0 }
                ] 
            document.getElementsByClassName('systemSun')[0].animate(keyframes1,keyframesOptions)
            setTimeout(function () {
                self.sunnum.changeSunNum()
                document.getElementsByClassName('systemSun')[0].style.left = Math.floor(Math.random() * 200 + 300) + 'px'
                document.getElementsByClassName('systemSun')[0].style.top = '-100px'
            }, 2700)
        }, 1000 * self.sunTimer_difference)

        self.zombieTimer = setInterval(function () {
            let idx = self.zombies_iMax - self.zombies_idx - 1
            if(self.zombies_idx === self.zombies_iMax) { // 僵尸生成数量达到最大值，清除定时器
                return clearInterval(self.zombieTimer)
            }
            if(self.zombies[idx]) {
                self.zombies[idx].state = self.zombies[idx].state_RUN
            }
            self.zombies_idx++
        },1000 * self.zombieTimer_difference)
    }

    setCars(cars_info){
        let self=this
        for(let car of cars_info.position){
            let info={
                x: cars_info.x,
                y: cars_info.y + 100 * (car.row - 1),
                row: car.row,
            }
            self.cars.push(Car.new(info))
        }
    }

    setCards(cards_info){
        let self=this
        for (let card of cards_info.position) {
            let info={
                name:card.name,
                row:card.row,
                sun_val:card.sun_val,
                timer_spacing: card.timer_spacing,
                y: cards_info.y + 60 * (card.row - 1),
            }
            self.cards.push(Card.new(info))
        }
    }


    //palnt or zombie
    setRoles(roles_info){
        let self=this,type = roles_info.type
        for (let role of roles_info.position){
            let info = {
                type: roles_info.type,
                section: role.section,
                x: roles_info.x + 80 * (role.col - 1),
                y: roles_info.y + 100 * (role.row - 1),
                col: role.col,
                row: role.row,
            }

            if(type==='plant'){
                self.plants.push(Plant.new(info))
            }else if(type==='zombie'){
                self.zombies.push(Zombie.new(info))
            }
        }
    }



    //===========================================
    start(){
        let self=this
        self.loading = Animation.new({type: 'loading'}, 'write', 55)
        self.sunnum = SunNum.new()
        self.setZombiesInfo()
        self.setCars(self.cars_info)
        self.setCards(self.cards_info)
        self.setRoles(self.plants_info)
        self.setRoles(self.zombies_info)

        self.game = Game.new()
    }
}


window._main=new Main()
window._main.start()


